<?php

class Report_IndexController extends Zend_Controller_Action
{       
    public function indexAction()
    {
        $report = 'Emms_Report_' . $this->getRequest()->getParam('Report');
        $reportObj = new $report();
        $reportSession = new Zend_Session_Namespace('Report'); 
        $form = $reportObj->getForm();
        $formClass = get_class($form);
        $columns = $form->getElement('Columns');
        if($columns)
            $columns->addMultiOptions(get_object_vars($reportObj->getHeaders()));
        if ($this->getRequest()->isPost()&&$form->isValid($this->getRequest()->getPost())) {
            $reportSession->$report = $this->getRequest()->getPost();
            $reportSession->$formClass = $this->getRequest()->getPost();
            $reportObj->setParams($this->getRequest()->getPost());
            $this->view->data = $reportObj->fetch();
            $this->view->headers = $reportObj->getHeaders($this->getRequest()->getPost('Columns'));
            $this->view->filename = $reportObj->getFilename($this->getRequest()->getPost());
            $this->view->headings = $reportObj->getHeadings();
            $this->view->footnotes = $reportObj->getFootnotes();
            $this->getHelper('layout')->setLayout('csv2');
            $this->getHelper('viewRenderer')->setNoRender(true);
        } else {
            $form->setAttrib('title', $this->view->translate("Reports"));
            $form->setDescription($reportObj->getName());
            if(isset($reportSession->$report)) {
                $defaults = $reportSession->$report;
            } elseif(isset($reportSession->$formClass)) {
                $defaults = array_merge($reportSession->$formClass, 
                        array('Report' => $report, 'Columns' => array()));
            } else {
                $defaults = array('Report' => $report);
            }
            $form->setDefaults($defaults);
            $this->view->form = $form;
            $this->_helper->layout->setLayout('report');
        }
    }
    
    public function listAction()
    {
        $this->_helper->layout->setLayout('left');
    }
}
